import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86185
 * Date: 2023-03-09
 * Time: 18:46
 */
public class test {
    public static void main1(String[] args) {
        //arr 引用变量，在这个变量中存储的是地址
        int[] arr = {1,2,3,4,5};
        int[] arr2 = null;//arr2引用 不指向任何对象
        //System.out.println(arr2[0]);//error
        //System.out.println(arr2.length);//error
        //NullPointerException 空指针异常
        //null是空指针，不指向任何对象
        arr2 = arr;
        //arr2[0]=99;
        String ret1 = Arrays.toString(arr);
        String ret2 = Arrays.toString(arr2);

        System.out.println(ret1);
        System.out.println(ret2);
        //arr2这个引用指向了arr这个引用所指向的对象
        //两个引用指向同一个对象
    }

    public static void main2(String[] args) {
        int[] arr = {1,2,3,4,5,6,7,8};
        String ret = myToString(arr);
        System.out.println(ret);
        int ret2 = findVal1(arr,5);
        System.out.println(ret2);
        int ret3 = findVal2(arr,8);
        System.out.println(ret3);
    }
    public static int findVal2(int[] arr,int key) {
        //二分
        int right = arr.length;
        int left = 0;
        while(right >= left) {
            int mid = (right + left) / 2;
            if(arr[mid] > key) {
                right = mid - 1;
            } else if (arr[mid] < key) {
                left = mid + 1;
            } else if (arr[mid] == key)
                return mid;
            }
        return -1;
    }


    public static int findVal1(int[] arr,int key) {
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] == key) {
                return i;
            }
        }
        return -1;
    }
    public static String myToString(int[] arr) {

        String ret = "[";
        if(arr == null) {
            return null;
        }
        if(arr.length==0) {
            ret += "]";
            return ret;
        }
        for (int i = 0; i < arr.length; i++) {
            ret += arr[i];
            if(i != arr.length - 1) {
                ret += ",";
            }
        }
        ret += "]";
        return ret;
    }

    public static void main3(String[] args) {
        int[] arr = {1,2,13,4,5,16,7};
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));
        int ret = Arrays.binarySearch(arr,13);
        System.out.println(ret);
    }
//    public static void bubbleSort(int[] arr) {
//
//    }

    public static int[] copy(int[] arr) {
        int[] copyarr = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            copyarr[i] = arr[i];
        }
        return copyarr;
    }

    public static void main5(String[] args) {
        int[] arr = {1,2,13,4,5,16,7};
        Arrays.sort(arr);
        int[] ret = copy(arr);
        System.out.println(Arrays.toString(ret));
        int[] ret2 = Arrays.copyOf(arr,arr.length*2);
        System.out.println(Arrays.toString(ret2));
        int[] ret3 = Arrays.copyOfRange(arr,1,5);
        System.out.println(Arrays.toString(ret3));
        int[] ret4 = new int[arr.length+1];
        System.arraycopy(arr,0,ret4,1,arr.length);
        System.out.println(Arrays.toString(ret4));
    }
    public static void reverseArr(int[] arr) {
        int i=0;
        int j=arr.length-1;
        while(i<j) {
            int tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
            i++;
            j--;
        }
    }

    public static void main6(String[] args) {
        int[] arr = {1,2,13,4,5,16,7};
        reverseArr(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void main8(String[] args) {
        int[][] arr = {{1,2,3},{4,5,6}};
//        System.out.println(arr[0]);
//        System.out.println(arr[1]);
        System.out.println(arr.length);
        System.out.println(arr[0].length);
        System.out.println("===============");
        for (int[] tmp: arr) {
            for (int x:
                 tmp) {
                System.out.print(x+" ");
            }
        }
        System.out.println("===============");
        String ret = Arrays.deepToString(arr);
        System.out.println(ret);
    }

    public static void main7(String[] args) {
        int[][] arr = {{1,2,3},{4,5,6}};
        int[][] arr1 = new int[2][3];
        int[][] arr2 = new int[][]{{1,2,3},{4,5,6}};

        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.print(arr[i][j]+" ");
            }
            System.out.println();
        }
    }

    public static void main9(String[] args) {
        int[][] arr = new int[2][];//不可以省略行，可以省略列
        arr[0] = new int[3];
        arr[1] = new int[5];
        System.out.println(arr[0].length);
        System.out.println(arr[1].length);
    }

    public static void main10(String[] args) {
        int[] arr = {1,2,3,4,5,6};
        Arrays.fill(arr,0,2,9);
        System.out.println(Arrays.toString(arr));
    }

    public static void main11(String[] args) {
        int[] arr = {1,2,3,4,5,6};
        for (int i = 0; i < arr.length - 1; i++) {
            int flag = 0;
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if(arr[j] % 2 == 0 && arr[j + 1] % 2 != 0) {
                    int t = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = t;
                    flag++;
                }
            }
            if(flag == 0) {
                break;
            }
        }
        System.out.println(Arrays.toString(arr));
    }

    public static void main12(String[] args) {
        int[] arr = {1,2,3,4,7,9,11};
        int t = 9;
        int right = arr.length - 1;
        int left = 0;
        int flag = 0;
        while(right >= left) {
            int mid = (right + left) / 2;
            if(arr[mid] > t) {
                right = mid + 1;
            } else if (arr[mid] < t) {
                left = mid + 1;
            } else {
                System.out.println(mid);
                flag = 1;
                break;
            }
        }
        if(flag == 0) {
            System.out.println(false);
        }
    }

    public static void main13(String[] args) {
        int[] arr = {10,5,6,8,9,6,2,4};
        for (int i = 0; i < arr.length - 1; i++) {
            int flag = 0;
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if(arr[j] > arr[j + 1]) {
                    int t = arr[j];
                    arr[j] = arr [j + 1];
                    arr[j + 1] = t;
                    flag = 1;
                }
            }
            if(flag == 0) {
                break;
            }
        }
        System.out.println(Arrays.toString(arr));
    }

    public static int[] addToTarget(int[] nums,int t) {
        int[] arr = new int[2];
        for (int i = 0; i < nums.length - 1; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if(nums[i] + nums[j] == t) {
                    arr[0] = i;
                    arr[1] = j;
                    return arr;
                }
            }
        }
        return null;
    }
    public static void main14(String[] args) {
        int[] nums = {2,7,11,15};
        int target = 17;
        int[] ret = addToTarget(nums,target);
        System.out.println(Arrays.toString(ret));
    }

    public static void main15(String[] args) {
        int[] arr = {4,1,2,1,2};
        Arrays.sort(arr);
        for (int i = 0; i < arr.length; i++) {
            if(i == arr.length - 1) {
                System.out.println(arr[i]);
                break;
            } else if(arr[i] == arr[i + 1]) {
                i++;
            } else if (arr[i] != arr[i + 1]) {
                System.out.println(arr[i]);
                break;
            }
        }
    }

    public static void main16(String[] args) {
        int[] arr = {2,2,1,1,1,2,2};
        Arrays.sort(arr);
        int l = arr.length / 2;
        int count = 1;
        for (int i = 0; i < arr.length - 1; i++) {
            if(arr[i] == arr[i + 1]) {
                count++;
                if(count > l) {
                    System.out.println(arr[i]);
                    break;
                }
            }else {
                if(count > l) {
                    System.out.println(arr[i]);
                    break;
                } else {
                    count = 1;
                }
            }
        }
    }
    public static boolean judgeArr (int[] arr) {
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] % 2 == 1) {
                count++;
            }
            if(count == 3) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        int[] arr = {1,2,34,3,4,5,7,23,12};
        boolean ret = judgeArr(arr);
        System.out.println(ret);
    }

}
